#include "OptionsState.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include <fstream>
using namespace std;
#include "../Game.h"
#include "MainMenuState.h"
#include "SaveGameState.h"

COptionsState* COptionsState::GetInstance(void)
{
	static COptionsState s_Instance;
	return &s_Instance;
}

void COptionsState::Enter(void)
{
	SGD::GraphicsManager::GetInstance()->SetClearColor({ 0, 0, 0 });
	boom = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/1407_IForgot_transistionscreen.png");
	SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetBoomSound(), false);
	
	std::string filePath = Game::GetInstance()->GetSavePath();

	filePath += "options.txt";

	ifstream file(filePath, ios_base::in);
	if (file.is_open())
	{
		while (true)
		{
			if (file.eof())
				break;
			file >> fullscreen >> sounds >> music;
			file.ignore(INT_MAX, '\n');
		}
	}
	Game::GetInstance()->SetFullscreen(fullscreen);
	file.close();	
}

void COptionsState::Exit(void)
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(boom);
	flash = true;
	Time = 0.0f;
	
	
	std::string filePath = Game::GetInstance()->GetSavePath();
	
	filePath += "options.txt";

	SGD::AudioManager::GetInstance()->SetAudioVolume(Game::GetInstance()->GetMenuMusic(), music);

	ofstream file(filePath);
	if (file.is_open())
	{		
		file << fullscreen << "\n" << sounds << "\n" << music;		
		file.close();
	}
}

bool COptionsState::Input(void)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	if (pInput->IsKeyPressed(SGD::Key::Escape) == true || pInput->IsButtonPressed(0, 1) == true)
	{
		Game::GetInstance()->ChangeState(CMainMenuState::GetInstance());
		return true;
	}	

	if (pInput->IsKeyPressed(SGD::Key::Down) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Down) == true
		)
	{
		++m_nCursor;

		// Wrap around
		if (m_nCursor > 3)
			m_nCursor = 0;
	}
	else if (pInput->IsKeyPressed(SGD::Key::Up) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Up) == true
		)
	{
		--m_nCursor;

		// Wrap around
		if (m_nCursor < 0)
			m_nCursor = 3;
	}

	if ((pInput->IsButtonPressed(0, 0) == true || pInput->IsKeyPressed(SGD::Key::Enter) == true) && m_nCursor == 3)
	{
		Game::GetInstance()->ChangeState(CSaveGameState::GetInstance());
		return true;
	}
	
	return true;
}

void COptionsState::Update(float elapsedTime)
{
	SGD::AudioManager* pAudio = SGD::AudioManager::GetInstance();
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();
	const Font* pFont = Game::GetInstance()->GetFont();
		
	SGD::Vector c = pInput->GetLeftJoystick(0);

	Time += elapsedTime;

	if (c.y > 0 && Time >= 0.2f)
	{
		++m_nCursor;

		// Wrap around
		if (m_nCursor > 3)
			m_nCursor = 0;
		Time = 0.0f;
	}

	else if (c.y < 0 && Time >= 0.2f)
	{
		--m_nCursor;

		// Wrap around
		if (m_nCursor < 0)
			m_nCursor = 3;
		Time = 0.0f;
	}

	if (flash == false)
	{
		if (m_nCursor == 0)
		{
			if (pInput->IsKeyPressed(SGD::Key::LeftArrow) == true || pInput->IsDPadPressed(0, SGD::DPad::Left) == true)
			{
				music -= 10;
				if (music < 0)
				{
					music = 0;
				}
			}
			if (pInput->IsKeyPressed(SGD::Key::RightArrow) == true || pInput->IsDPadPressed(0, SGD::DPad::Right) == true)
			{
				music += 10;
				if (music > 100)
				{
					music = 100;
				}
			}



			if (music == 100)
			{
				pFont->Draw("Music		100", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 90)
			{
				pFont->Draw("Music		90", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 80)
			{
				pFont->Draw("Music		80", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 70)
			{
				pFont->Draw("Music		70", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 60)
			{
				pFont->Draw("Music		60", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 50)
			{
				pFont->Draw("Music		50", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 40)
			{
				pFont->Draw("Music		40", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 30)
			{
				pFont->Draw("Music		30", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 20)
			{
				pFont->Draw("Music		20", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 10)
			{
				pFont->Draw("Music		10", { 1, 100 }, 1, { 0, 255, 0 });
			}
			else if (music == 0)
			{
				pFont->Draw("Music		0", { 1, 100 }, 1, { 0, 255, 0 });
			}
			pAudio->SetAudioVolume(Game::GetInstance()->GetMenuMusic(), music);
		}
		else
		{
			if (music == 100)
			{
				pFont->Draw("Music		100", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 90)
			{
				pFont->Draw("Music		90", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 80)
			{
				pFont->Draw("Music		80", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 70)
			{
				pFont->Draw("Music		70", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 60)
			{
				pFont->Draw("Music		60", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 50)
			{
				pFont->Draw("Music		50", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 40)
			{
				pFont->Draw("Music		40", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 30)
			{
				pFont->Draw("Music		30", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 20)
			{
				pFont->Draw("Music		20", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 10)
			{
				pFont->Draw("Music		10", { 1, 100 }, 1, { 255, 255, 255 });
			}
			else if (music == 0)
			{
				pFont->Draw("Music		0", { 1, 100 }, 1, { 255, 255, 255 });
			}
		}

		if (m_nCursor == 1)
		{
			if (pInput->IsKeyPressed(SGD::Key::LeftArrow) == true || pInput->IsDPadPressed(0, SGD::DPad::Left) == true)
			{
				sounds -= 10;
				if (sounds < 0)
				{
					sounds = 0;
				}
				pAudio->PlayAudio(Game::GetInstance()->Getcreatureatk(), false);
			}
			if (pInput->IsKeyPressed(SGD::Key::RightArrow) == true || pInput->IsDPadPressed(0, SGD::DPad::Right) == true)
			{
				sounds += 10;
				if (sounds > 100)
				{
					sounds = 100;
				}
				pAudio->PlayAudio(Game::GetInstance()->Getcreatureatk(), false);
			}
			if (sounds == 100)
			{
				pFont->Draw("Soundfxs	100", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 90)
			{
				pFont->Draw("Soundfxs	90", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 80)
			{
				pFont->Draw("Soundfxs	80", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 70)
			{
				pFont->Draw("Soundfxs	70", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 60)
			{
				pFont->Draw("Soundfxs	60", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 50)
			{
				pFont->Draw("Soundfxs	50", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 40)
			{
				pFont->Draw("Soundfxs	40", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 30)
			{
				pFont->Draw("Soundfxs	30", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 20)
			{
				pFont->Draw("Soundfxs	20", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 10)
			{
				pFont->Draw("Soundfxs	10", { 1, 200 }, 1, { 0, 255, 0 });
			}
			else if (sounds == 0)
			{
				pFont->Draw("Soundfxs	0", { 1, 200 }, 1, { 0, 255, 0 });
			}
			pAudio->SetAudioVolume(Game::GetInstance()->Getcreatureatk(), sounds);
			pAudio->SetAudioVolume(Game::GetInstance()->Getcreaturedie(), sounds);
			pAudio->SetAudioVolume(Game::GetInstance()->Getcreaturegrowl(), sounds);
			pAudio->SetAudioVolume(Game::GetInstance()->Gethit(), sounds);
			pAudio->SetAudioVolume(Game::GetInstance()->GetBoomSound(), sounds);
			pAudio->SetAudioVolume(Game::GetInstance()->GetMenusound(), sounds);

		}
		else
		{
			if (sounds == 100)
			{
				pFont->Draw("Soundfxs	100", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 90)
			{
				pFont->Draw("Soundfxs	90", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 80)
			{
				pFont->Draw("Soundfxs	80", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 70)
			{
				pFont->Draw("Soundfxs	70", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 60)
			{
				pFont->Draw("Soundfxs	60", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 50)
			{
				pFont->Draw("Soundfxs	50", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 40)
			{
				pFont->Draw("Soundfxs	40", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 30)
			{
				pFont->Draw("Soundfxs	30", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 20)
			{
				pFont->Draw("Soundfxs	20", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 10)
			{
				pFont->Draw("Soundfxs	10", { 1, 200 }, 1, { 255, 255, 255 });
			}
			else if (sounds == 0)
			{
				pFont->Draw("Soundfxs	0", { 1, 200 }, 1, { 255, 255, 255 });
			}
		}

		if (m_nCursor == 2)
		{
			if (pInput->IsKeyPressed(SGD::Key::LeftArrow) == true || pInput->IsDPadPressed(0, SGD::DPad::Left) == true)
			{
				fullscreen = false;
				Game::GetInstance()->SetFullscreen(fullscreen);
				SGD::GraphicsManager::GetInstance()->Resize(SGD::Size{ Game::GetInstance()->GetScreenWidth(), Game::GetInstance()->GetScreenHeight() }, true);
			}

			if (pInput->IsKeyPressed(SGD::Key::RightArrow) == true || pInput->IsDPadPressed(0, SGD::DPad::Right) == true)
			{
				fullscreen = true;
				Game::GetInstance()->SetFullscreen(fullscreen);
				SGD::GraphicsManager::GetInstance()->Resize(SGD::Size{ Game::GetInstance()->GetScreenWidth(), Game::GetInstance()->GetScreenHeight() }, false);
			}
		}

		if (m_nCursor == 2)
		{
			if (!fullscreen)
			{
				pFont->Draw("FullScreen	OFF", { 0, 300 }, 1, { 0, 255, 0 });
			}
			else
			{
				pFont->Draw("FullScreen	ON", { 0, 300 }, 1, { 0, 255, 0 });
			}
		}
		else
		{
			if (!fullscreen)
			{
				pFont->Draw("FullScreen	OFF", { 0, 300 }, 1, { 255, 255, 255 });
			}
			else
			{
				pFont->Draw("FullScreen	ON", { 0, 300 }, 1, { 255, 255, 255 });
			}
		}
	}
	if (Time > 1.2f)
	{
		flash = false;
	}
}

void COptionsState::Render(void)
{
	const Font* pFont = Game::GetInstance()->GetFont();

	Game* pGame = Game::GetInstance();

	float darkOffset = pGame->DarkOff();
	float medOffset = pGame->MedOff();
	float lightOffset = pGame->LightOff();

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset - 805, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset - 805, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset - 805, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	if (flash)
		SGD::GraphicsManager::GetInstance()->DrawTexture(boom, { 0, 0 }, 0.0f, {}, { 255, 255, 255 }, { 1, 1 });
	else
	{
		pFont->Draw("Options", { 250, 1 }, 1, { 255, 255, 255 });
		if (m_nCursor == 3)
			pFont->Draw("Save Slot", { 0, 400 }, 1, { 0, 255, 0 });
		else
			pFont->Draw("Save Slot", { 0, 400 }, 1, { 255, 255, 255 });
	}
}